package com.omi.iberolactalpreventa;

import java.util.ArrayList;

import com.omi.iberolactalpreventa.objects.AdapterItmProducto;
import com.omi.iberolactalpreventa.objects.ItmProducto;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;


public class Productos extends Activity {
	private EditText txtbuscprods;
	private ListView lstprods;
	private Cursor c;
	
	 @SuppressLint("NewApi")
	@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_productos);
			txtbuscprods = (EditText) findViewById(R.id.txtbuscprods);
			lstprods = (ListView) findViewById(R.id.lstprods);
			
			//EVENTOS:
			txtbuscprods.addTextChangedListener(new TextWatcher() {
				
				@Override
				public void onTextChanged(CharSequence s, int start, int before, int count) {
					cargarProductos();
				}
				
				@Override
				public void beforeTextChanged(CharSequence s, int start, int count,
						int after) {
				}
				
				@Override
				public void afterTextChanged(Editable s) {
				}
			});
			
			lstprods.setOnItemClickListener(new OnItemClickListener() {

				@SuppressLint("NewApi")
				@Override
				public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
					ItmProducto prod = (ItmProducto)parent.getItemAtPosition(position);
					Modulebase.prodTemp = prod;
					finish();
				}
			});
			
		cargarProductos();
		getActionBar().setDisplayHomeAsUpEnabled(true);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
	 }
	
	 @Override
	 public void onBackPressed() {
	 	finish();
	 }
	 
		private void cargarProductos()
		{
			c = Modulebase.db.ejecutarSelect(new String[]{"Numero","Descripcion","Descripcion2"}, "PRODUCTO", "Descripcion LIKE ?",new String[]{"%"+txtbuscprods.getText()+"%"});
			
			if(c!=null && c.moveToFirst())
			{
				ArrayList<ItmProducto> prods = new ArrayList<ItmProducto>();
				
				do
				{
					ItmProducto prod = new ItmProducto(c.getString(0), c.getString(1), c.getString(2));
					prods.add(prod);
				}while(c.moveToNext());
				
				AdapterItmProducto adptr = new AdapterItmProducto(this, prods);
				lstprods.setAdapter(adptr);
			}
		}
	
		 @Override
	        public boolean onOptionsItemSelected(MenuItem item) {
	                switch (item.getItemId()) {
	                case android.R.id.home:
	                 finish();
	                 return true;
	                default: 
	                 return super.onOptionsItemSelected(item);      
	                }
	        }
}
